package HomeView.view;

import HomeView.domain.House;
import HomeView.service.HomeService;
import HomeView.utlis.Utility;

public class HouseView {


    private boolean loop = true;
    private String key = "";

    private HomeService homeService = new HomeService(10);

//    房屋出租系统主菜单
    public void mainmenu(){

        do {

            System.out.println("-------------房屋出租系统-------------");
            System.out.println("\t\t\t 1 新 增 房 源");
            System.out.println("\t\t\t 2 查 找 房 屋");
            System.out.println("\t\t\t 3 删 除 房 屋");
            System.out.println("\t\t\t 4 修 改 稿 房 屋 信 息");
            System.out.println("\t\t\t 5 房 屋 列 表");
            System.out.println("\t\t\t 6 退      出");
            System.out.print("请选择(1-6)：");
            key = Utility.readString(1);
            switch (key){
                case "1":
                    addHouse();
//                    System.out.println("1 新 增 房 源");
                    break;
                    
                case "2":
                    System.out.println("2 查 找 房 屋");
                    selectList();

                    break;
                case "3":
//                    System.out.println("3 删 除 房 屋");

                    delHouse();

                    break;
                case "4":
                    update();
//                    System.out.println("4 修 改 稿 房 屋 信 息");
                    break;
                case "5":
//                    System.out.println("5 房 屋 列 表");
                    listHouse();
                    break;
                case "6":
//                    System.out.println("6 退      出");
                     char choice = Utility.readConfirmSelection();
                    if(choice == 'Y'){
                        System.out.println("=============退出房屋系统=============");
                        return;
                    }else {
                        System.out.println("=============放弃退出房屋系统=============");
                    }
                    break;
                default:
                    System.out.println("请输入正确的选项");
                    break;
            }

        }while (loop);



    }

    public void addHouse(){

        System.out.println("--------------添加房屋---------------");
        System.out.print("姓名：");
        String name = Utility.readString(6,"叶真");
        System.out.print("电话：");
        String phone = Utility.readString(11,"12345678901");
        System.out.print("地址：");
        String address = Utility.readString(20,"北京张家口");
        System.out.print("租金：");
        double monthlyRent = Utility.readInt(5000);
        System.out.print("状态：");
        String state = Utility.readString(3,"未出租");
        House house = new House(0, name, phone, address, (int) monthlyRent, state);
        if(homeService.add(house)){
            System.out.println("==============添加房屋成功===============");
        }else{
            System.out.println("==============添加房屋失败===============");
        }

    }

    public void delHouse(){
        System.out.println("=============删除房屋信息=============");
        System.out.println("请输入待删除房屋的编号(-1 退出)");
        int delId = Utility.readInt(-1);
        if(delId == -1){
            System.out.println("=============放弃删除房屋信息=============");
            return;
        }
//        del();

//        System.out.println("请确认是否删除（Y/N）,小心选择：");
        char choice = Utility.readConfirmSelection();
//        System.out.println("choice==========："+(choice == 'Y'));
//        System.out.println("delId==========："+ delId );
//        System.out.println("choice==========："+ choice );
        if(choice == 'Y'){
            if (homeService.del(delId)){
                System.out.println("==============删除房屋信息成功===============");
            }else {
                System.out.println("==============房屋编号不存在，删除失败===============");
            }
        }else {
            System.out.println("=============放弃删除房屋信息=============");
        }

    }

    public void selectList(){
//        System.out.println("==========请输入查询的房屋编号=========");
        System.out.println("=============查询房屋信息开始=============");
        System.out.println("请输入查询房屋的编号(-1 退出)");
        int selectId = Utility.readInt(-1);
        if(selectId == -1){
            System.out.println("=============放弃查询房屋信息=============");
            return;
        }

        char choice = Utility.readConfirmSelection();
        if(choice == 'Y'){
            System.out.println();
            House[] house = homeService.select(selectId);
            if(house.length<0){
                System.out.println("======当前编号有误====");
            }

            System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态");
            for (int i = 0; i < house.length; i++) {
                if(house[i] == null){
                    break;
                }
                System.out.println(house[i]);
            }
            System.out.println("=============查询房屋信息结束=============");

        }else {
            System.out.println("=============放弃查询房屋信息=============");
        }



    }


    public void listHouse(){

        System.out.println("-------------房屋列表-------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态");

        House[] house = homeService.list();
        for (int i = 0; i < house.length; i++) {
            if(house[i] == null){
                break;
            }
            System.out.println(house[i]);
        }

        System.out.println("-------------房屋列表完成-------------");
    }


    public void update(){

        System.out.println("=============修改房屋信息=============");
        System.out.println("请输入待修改房屋的编号(-1 退出)");
        int updateId = Utility.readInt(-1);
        if (updateId == -1){
            System.out.println("=============放弃修改房屋信息=============");
            return;
        }

        System.out.println("请输入修改后的房屋信息");
        House house = homeService.findId(updateId);
//        System.out.println("house------------------>>>"+house);
        System.out.print("姓名：("+house.getName()+")");
        String name = Utility.readString(6,house.getName());
        System.out.print("电话：("+house.getPhone()+")");
        String phone = Utility.readString(11,house.getPhone());
        System.out.print("地址：("+house.getAddress()+")");
        String address = Utility.readString(20,house.getAddress());
        System.out.print("租金：("+house.getMonthlyRent()+")");
        int monthlyRent = Utility.readInt(house.getMonthlyRent());
        System.out.print("状态：("+house.getState()+")");
        String state = Utility.readString(3,house.getState());

        house = new House(updateId, name, phone, address, monthlyRent, state);
        if(homeService.update(updateId,house)){
            System.out.println("=============修改房屋信息成功=============");
        }else {
            System.out.println("=============修改房屋信息失败=============");
        }



    }

//    public void list(){
//
//
//
//    }


}
